package com.lemon.smartsafety.ui.activity.hidden;

import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bigkoo.pickerview.OptionsPickerView;
import com.lemon.android.http.Field;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.BuildConfig;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.common.EvidenceActionSheetType;
import com.lemon.smartsafety.entity.common.UploadFileItem;
import com.lemon.smartsafety.entity.event.hidden.HiddenMainRefreshEvent;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.hidden.HiddenTypePickViewVo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.HiddenServices;
import com.lemon.smartsafetynew.ui.activity.base.PhotoSelectBaseActivity;
import com.lemon.smartsafety.ui.adapter.common.SelectEvidenceAdapter;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.safecloud.greendao.HiddenDangerInfo;
import com.lemon.safecloud.greendao.HiddenDangerInfoDao;
import com.lemon.safecloud.greendao.HiddenLevel;
import com.lemon.safecloud.greendao.HiddenType;
import com.lemon.safecloud.greendao.SelfCheckType;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import org.greenrobot.eventbus.Subscribe;

import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Cooliean on 16/1/24.
 */
public class HiddenPropActivity extends PhotoSelectBaseActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(HiddenPropActivity.class);


    private enum DialogType {
        TYPE_CATE, TYPE_LEVEL;
    }


    @Bind(R.id.mainContainer)
    View mainContainer;


    @Bind(R.id.editCheckPart)
    MaterialEditText editCheckPart;


    @Bind(R.id.rowDatePicker)
    TableRow rowDatePicker;


    @Bind(R.id.metCheckDate)
    MaterialEditText metCheckDate;


    @Bind(R.id.rowCheckType)
    TableRow rowCheckType;


    @Bind(R.id.metCheckType)
    MaterialEditText metCheckType;


    @Bind(R.id.editCheckPerson)
    MaterialEditText editCheckPerson;


    @Bind(R.id.rowHiddenCategory)
    TableRow rowHiddenCategory;


    @Bind(R.id.metHiddenCate)
    MaterialEditText metHiddenCate;


    @Bind(R.id.rowHiddenLevel)
    TableRow rowHiddenLevel;


    @Bind(R.id.metHiddenLevel)
    MaterialEditText metHiddenLevel;


    @Bind(R.id.editHiddenContent)
    MaterialEditText editHiddenContent;

    @Bind(R.id.chkIsReport)
    AppCompatCheckBox chkIsReport;


    @Bind(R.id.editReformDepartment)
    MaterialEditText editReformDepartment;


    @Bind(R.id.editReformPerson)
    MaterialEditText editReformPerson;


    @Bind(R.id.rowDatePickerFinish)
    TableRow rowDatePickerFinish;


    @Bind(R.id.metReformFinishDate)
    MaterialEditText metReformFinishDate;


    @Bind(R.id.editReformFund)
    MaterialEditText editReformFund;


    @Bind(R.id.editReformContent)
    MaterialEditText editReformContent;


    @Bind(R.id.chkIsReformFinished)
    AppCompatCheckBox chkIsReformFinished;



    @Bind(R.id.tl_report)
    TableLayout reportMainLayout;


    @Bind(R.id.tl_reform)
    TableLayout reformMainLayout;

    @Bind(R.id.tr_button)
    TableRow trButton;


    /**
     * 类型
     */
    private String[] aSelfTypeName;
    private int[]    aSelfTypeId;
    private int typeWhich = -1;
    /**
     * 级别
     */
    private String[] aHiddenLevelName;
    private int[]    aHiddenLevelId;
    private int levelWhich = -1;


    private MaterialDialog.Builder mMaterialDialog;

    /**
     * 隐患分类选择
     */
    private OptionsPickerView<HiddenType> mHiddenCategoryView;
    private HiddenTypePickViewVo          mPickViewVo;
    private List<HiddenType>              mHiddenTypes;
    private int mHiddenTypePosition1 = -1, mHiddenTypePosition2 = -1;


    /****************************************************************************************************************
     * 表单字段
     ****************************************************************************************************************/

//    private String sHiddenDangerId = "";
    private String txtCheckPart           = "";
    private String txtCheckDateValue      = "";
    private String txtSelfCheckId         = "";
    private String txtTypeId              = "";
    private String txtLevelId             = "";
    private String txtHiddenDangerContent = "";
    private String txtCheckUserName       = "";
    private String txtReformDepartment    = "";
    private String txtReformPerson        = "";
    private String txtPlanFinishedDate    = "";
    private String txtReformFund          = "";
    private String txtReformContent       = "";
    private String txtCheckTypeValue      = "self";


    /**
     * 编辑用的地方
     */

    /**
     * 是否编辑
     */
    private boolean          isEdit      = false;

    private HiddenDangerInfo mHiddenInfo = null;

    private Long iHiddenDangerId = 0L;

    private HiddenDangerInfoDao mHiddenDangerInfoDao;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_hidden_prop;
    }

    @Override
    protected void initViews() {
        super.initViews();


        //这里是测试用的默认值
        initTestValue();


        //编辑要使用的地方
        mHiddenDangerInfoDao = AppService.getDBHelper().getDaoSession().getHiddenDangerInfoDao();
        iHiddenDangerId = getIntent().getLongExtra("info_id", 0L);
        if (iHiddenDangerId != 0L) {
            List<HiddenDangerInfo> result = mHiddenDangerInfoDao.queryBuilder().where(HiddenDangerInfoDao.Properties.HiddenDangerId.eq(iHiddenDangerId)).list();
            if (result != null && result.size() == 1) {
                mHiddenInfo = mHiddenDangerInfoDao.queryBuilder().where(HiddenDangerInfoDao.Properties.HiddenDangerId.eq(iHiddenDangerId)).list().get(0);
            }
        }
        if (mHiddenInfo != null && iHiddenDangerId != 0L) {
            isEdit = true;
            initEditView(mHiddenInfo);



            if (mHiddenInfo.getStatus() == 1) {
                disableForm(reportMainLayout);
            }
            if(mHiddenInfo.getStatus() == 1 && mHiddenInfo.getIsReformfinished()) {
                disableForm(reportMainLayout);
                disableForm(reformMainLayout);
                trButton.setVisibility(View.GONE);
                mSelectEvidenceAdapter.setCanAdd(false);

            }
            /** 获取附件信息 */
            getHiddenAttestList(iHiddenDangerId);


        }
    }


    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.hidden_title);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(HiddenPropActivity.class);
    }


    @Override
    protected void initListener() {
        super.initListener();
        rowDatePicker.setOnClickListener(clickListener);
        metCheckDate.setOnClickListener(clickListener);
        rowDatePickerFinish.setOnClickListener(clickListener);
        metReformFinishDate.setOnClickListener(clickListener);

        rowCheckType.setOnClickListener(clickListener);
        metCheckType.setOnClickListener(clickListener);


        rowHiddenLevel.setOnClickListener(clickListener);
        metHiddenLevel.setOnClickListener(clickListener);
        rowHiddenCategory.setOnClickListener(clickListener);
        metHiddenCate.setOnClickListener(clickListener);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.rowDatePicker || v.getId() == R.id.metCheckDate) {
                pickCheckDate();
            } else if (v.getId() == R.id.rowDatePickerFinish || v.getId() == R.id.metReformFinishDate) {
                pickReformDate();
            } else if (v.getId() == R.id.rowCheckType || v.getId() == R.id.metCheckType) {//隐患检查类型
                if (aSelfTypeName == null || aSelfTypeId == null) {
                    HiddenServices.init().getHiddenSelfCheckType();
                } else {
                    showSelfCheckTypeDialog();
                }
            } else if (v.getId() == R.id.rowHiddenLevel || v.getId() == R.id.metHiddenLevel) {//隐患级别
                if (aHiddenLevelId == null || aHiddenLevelName == null) {
                    HiddenServices.init().getHiddenLevel();
                } else {
                    showHiddenLevelDialog();
                }
            } else if (v.getId() == R.id.rowHiddenCategory || v.getId() == R.id.metHiddenCate) {//隐患分类
                getHiddenCategoryData();
            }
        }
    };

    /**
     * 显示自查类型Dialog
     */
    private void showSelfCheckTypeDialog() {
        showSingleDialog(R.string.hidden_prop_dialog_self_check_type, aSelfTypeName, aSelfTypeId, DialogType.TYPE_CATE, metCheckType, typeWhich);
    }

    /**
     * 显示隐患级别Dialog
     */
    private void showHiddenLevelDialog() {
        showSingleDialog(R.string.hidden_prop_dialog_hidden_level, aHiddenLevelName, aHiddenLevelId, DialogType.TYPE_LEVEL, metHiddenLevel, levelWhich);
    }

    /**
     * 选择隐患分类选择器
     */
    private void getHiddenCategoryData() {
        if (mHiddenTypes != null && mHiddenTypes.size() != 0) {//节约网络请求的次数，这里如果请求过一次了就不到网络去取了
            showHiddenCategoryPickView(mHiddenTypes);
        } else {
            HiddenServices.init().getHiddenCategory();
        }
    }


    private void getHiddenAttestList(Long hiddenDangerId) {
        HiddenServices.init().getHiddenDangerAttestList(hiddenDangerId.toString());
    }


    /**
     * 窗口提示（单选）
     *
     * @param title       窗口标题
     * @param items       问题数据
     * @param ints        ID数据
     * @param dialogType  窗口类型
     * @param textView    接受结果的View
     * @param oldPosition 历史位置
     */
    public void showSingleDialog(int title, @NonNull final String[] items, @NonNull final int[] ints, final DialogType dialogType, final TextView textView, int oldPosition) {
        if (oldPosition >= items.length || oldPosition == -1) {
            oldPosition = 0;
        }
        new MaterialDialog.Builder(this).title(title).items(items).itemsCallbackSingleChoice(oldPosition, new MaterialDialog.ListCallbackSingleChoice() {
            @Override
            public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                if (DialogType.TYPE_CATE == dialogType) {//自查类型
                    typeWhich = which;
                } else if (DialogType.TYPE_LEVEL == dialogType) {//隐患分类
                    levelWhich = which;
                }
                if (which != -1) {
                    textView.setText(items[which]);

                    textView.setTag(ints[which]);
                }
                /**
                 * If you use alwaysCallSingleChoiceCallback(), which is discussed below,
                 * returning false here won't allow the newly selected radio button to actually be selected.
                 **/
                return true;
            }
        }).positiveText(R.string.sure_str).show();
    }


    /**
     * 检查时间
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                int    month = (++monthOfYear);
                String date  = year + "-" + month + "-" + dayOfMonth;
                if (month < 10) {
                    date = year + "-0" + month + "-" + dayOfMonth;
                }
                metCheckDate.setText(date);
            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

    private void pickReformDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                int    month = (++monthOfYear);
                String date  = year + "-" + month + "-" + dayOfMonth;
                if (month < 10) {
                    date = year + "-0" + month + "-" + dayOfMonth;
                }
                metReformFinishDate.setText(date);
            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_REFORM_DATE);
    }

    private OptionsPickerView getHiddenTypeOptionPickView(HiddenTypePickViewVo pickViewVo) {
        if (mHiddenCategoryView == null) {
            //选项选择器
            mHiddenCategoryView = new OptionsPickerView<HiddenType>(this);
            mHiddenCategoryView.setOnoptionsSelectListener(mOnOptionsSelectListener);
            mHiddenCategoryView.setPicker(pickViewVo.getOneLevel(), pickViewVo.getTwoLevel(), true);
            mHiddenCategoryView.setTitle(getString(R.string.hidden_prop_dialog_hidden_category));
            mHiddenCategoryView.setCyclic(false, true, true);
            //设置默认选中的三级项目
            //监听确定选择按钮
            if (mHiddenTypePosition1 >= pickViewVo.getOneLevel().size()) {
                mHiddenTypePosition1 = 0;
            }

            if (mHiddenTypePosition2 >= pickViewVo.getTwoLevel().size()) {
                mHiddenTypePosition2 = 0;
            }
            mHiddenCategoryView.setSelectOptions(mHiddenTypePosition1 == -1 ? 0 : mHiddenTypePosition1, mHiddenTypePosition2 == -1 ? 0 : mHiddenTypePosition2);
        }
        return mHiddenCategoryView;

    }


    /**
     * 显示隐患分类选择器
     *
     * @param items
     */
    private void showHiddenCategoryPickView(List<HiddenType> items) {
        mPickViewVo = HiddenServices.getHiddenTypePickViewVo(items);
        if (mPickViewVo != null && mPickViewVo.getOneLevel() != null && mPickViewVo.getTwoLevel() != null) {
            if (mPickViewVo.getOneLevel().size() == 0 || mPickViewVo.getTwoLevel().size() == 0) {
                mHUDUtil.showErrorTip(getString(R.string.error_str));
                return;
            }
            getHiddenTypeOptionPickView(mPickViewVo).show();
        }
    }

    /**
     * 选择器Listener
     */
    private OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int position1, int position2, int position3) {
            if (mPickViewVo == null || mPickViewVo.getOneLevel() == null || mPickViewVo.getTwoLevel() == null || mPickViewVo.getOneLevel().isEmpty() || mPickViewVo.getTwoLevel().isEmpty()) {
                return;
            }
            mHiddenTypePosition1 = position1;
            mHiddenTypePosition2 = position2;
            metHiddenCate.setText(getHiddenCategoryText(mPickViewVo, position1, position2));
        }
    };

    /**
     * 组装隐患分类的显示值
     *
     * @param pickViewVo
     * @param position1
     * @param position2
     * @return
     */
    private String getHiddenCategoryText(HiddenTypePickViewVo pickViewVo, int position1, int position2) {
        return pickViewVo.getOneLevel().get(position1).getPickerViewText() + "-" + pickViewVo.getTwoLevel().get(position1).get(position2).getPickerViewText();
    }

    /**
     * 获取选择隐患分类的ID
     *
     * @return
     */
    private String getHiddenCategoryId() {
        return mPickViewVo.getTwoLevel().get(mHiddenTypePosition1).get(mHiddenTypePosition2).getTypeId() + "";
    }


    /**
     * 表单验证
     *
     * @return
     */
    private boolean validateForm() {
        /**
         * 检查部位
         */
        txtCheckPart = StringUtils.nullValue(editCheckPart.getText().toString().trim());
        if (TextUtils.isEmpty(txtCheckPart)) {
            editCheckPart.setError(getString(R.string.hidden_prop_error_check_part_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_part_empty));
            return false;
        }


        /**
         * 检查时间
         */
        txtCheckDateValue = StringUtils.nullValue(metCheckDate.getText().toString().trim());
        if (TextUtils.isEmpty(txtCheckDateValue)) {
            metCheckDate.setError(getString(R.string.hidden_prop_error_check_date_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_date_empty));
            return false;
        }


        /**
         * 自查类型
         */
        if (TextUtils.isEmpty(metCheckType.getText().toString().trim())) {
            //metCheckType.setError(getString(R.string.hidden_prop_error_check_type_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_type_empty));
            return false;
        }
        txtSelfCheckId = StringUtils.nullValue(metCheckType.getTag().toString());


        /**
         * 检查人
         */
        txtCheckUserName = StringUtils.nullValue(editCheckPerson.getText().toString().trim());
        if (TextUtils.isEmpty(txtCheckUserName)) {
            editCheckPerson.setError(getString(R.string.hidden_prop_error_check_person_empty));
            return false;
        }


        /**
         * 隐患类型
         */
        if (TextUtils.isEmpty(metHiddenCate.getText().toString().trim())) {
            //metHiddenCate.setError(getString(R.string.hidden_prop_error_check_category_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_category_empty));
            return false;
        }
        if (mPickViewVo != null && mHiddenTypePosition1 != -1 && mHiddenTypePosition2 != -1) {
            txtTypeId = StringUtils.nullValue(getHiddenCategoryId());
        }

        /**
         * 隐患级别
         */
        if (TextUtils.isEmpty(metHiddenLevel.getText().toString().trim())) {
            //metHiddenLevel.setError(getString(R.string.hidden_prop_error_check_hidden_level_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_hidden_level_empty));
            return false;
        }
        txtLevelId = StringUtils.nullValue(metHiddenLevel.getTag().toString());


        /**
         * 隐患内容
         */
        txtHiddenDangerContent = StringUtils.nullValue(editHiddenContent.getText().toString().trim());
        if (TextUtils.isEmpty(txtHiddenDangerContent)) {
            editHiddenContent.setError(getString(R.string.hidden_prop_error_check_hidden_content_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_hidden_content_empty));
            return false;
        }


        /****************************************************************************************************************
         * 整改部分
         ****************************************************************************************************************/
        /**
         * 整改部门
         */
        txtReformDepartment = StringUtils.nullValue(editReformDepartment.getText().toString().trim());
        if (TextUtils.isEmpty(txtReformDepartment)) {
            editReformDepartment.setError(getString(R.string.hidden_prop_error_check_reform_department_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_reform_department_empty));
            return false;
        }

        /**
         * 整改负责人
         */
        txtReformPerson = StringUtils.nullValue(editReformPerson.getText().toString().trim());
        if (TextUtils.isEmpty(txtReformPerson)) {
            editReformPerson.setError(getString(R.string.hidden_prop_error_check_reform_preson_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_reform_preson_empty));
            return false;
        }


        /**
         * 整改时间
         */
        txtPlanFinishedDate = StringUtils.nullValue(metReformFinishDate.getText().toString().trim());
        if (TextUtils.isEmpty(txtPlanFinishedDate)) {
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_reform_date_empty));
            return false;
        }


        /**
         * 投资金额
         */
        txtReformFund = StringUtils.nullValue(editReformFund.getText().toString().trim());
        if (TextUtils.isEmpty(txtReformFund)) {
            editReformFund.setError(getString(R.string.hidden_prop_error_check_reform_fund_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_reform_fund_empty));
            return false;
        }


        /**
         * 整改措施
         */
        txtReformContent = StringUtils.nullValue(editReformContent.getText().toString().trim());
        if (TextUtils.isEmpty(txtReformContent)) {
            editReformContent.setError(getString(R.string.hidden_prop_error_check_reform_content_empty));
            mHUDUtil.showErrorTip(getString(R.string.hidden_prop_error_check_reform_content_empty));
            return false;
        }


        return true;
    }

    @OnClick(R.id.btn_submit)
    protected void btnSubmitAction() {
        if (!validateForm()) {
            return;
        }


        mMaterialDialog = getConfirmDialog(getString(R.string.is_submit), new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.POSITIVE) {
                    doPostAction();
                }
            }
        });
        mMaterialDialog.show();

    }

    private void doPostAction() {
        final LemonRequestParams params = new LemonRequestParams("hidden_danger_info", "hidden_danger_id", "");

        if (isEdit) {
            params.put("_txtIdValues", iHiddenDangerId + "");
            params.put("hidden_danger_id", iHiddenDangerId + "");
        } else {
            params.put("_txtIdValues", "");
            params.put("hidden_danger_id", "");
        }
        params.put("user_id", mSharedPreferencesHelper.getUserId());
        params.put("txtCheckPart", txtCheckPart, Field.TYPE_STRING);
        params.put("txtCheckDate", txtCheckDateValue, Field.TYPE_DATETIME);
        params.put("txtSelfCheckId", txtSelfCheckId, Field.TYPE_INT);
        params.put("txtTypeId", txtTypeId + "", Field.TYPE_INT);
        params.put("txtLevelId", txtLevelId + "", Field.TYPE_INT);
        params.put("txtHiddenDangerContent", txtHiddenDangerContent, Field.TYPE_STRING);
        params.put("txtCheckUserName", txtCheckUserName, Field.TYPE_STRING);
        params.put("txtRecordUserId", mSharedPreferencesHelper.getUserId() + "", Field.TYPE_STRING);
        params.put("txtRecordDate", DateUtils.getCurrentDate(DateUtils.dateFormatYMDHMS), Field.TYPE_DATETIME);
        params.put("chkStatus", chkIsReport.isChecked() ? "1" : "0" + "", Field.TYPE_INT);
        params.put("txtReformDepartment", txtReformDepartment, Field.TYPE_STRING);
        params.put("txtReformPerson", txtReformPerson, Field.TYPE_STRING);
        params.put("txtPlanFinishedDate", txtPlanFinishedDate, Field.TYPE_DATE);
        params.put("txtReformFund", txtReformFund, Field.TYPE_DOUBLE);
        params.put("txtReformContent", txtReformContent, Field.TYPE_STRING);
        params.put("chkIsReformFinished", chkIsReformFinished.isChecked() ? "1" : "0" + "", Field.TYPE_INT);
        params.put("metCheckType", txtCheckTypeValue, Field.TYPE_STRING);

        getUploadItems(new SelectEvidenceAdapter.CropCallBack<List<UploadFileItem>>() {
            @Override
            public void onStart() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mHUDUtil.showHUD();
                    }
                });
            }

            @Override
            public void onFinish(List<UploadFileItem> data) {
                if (data != null && data.size() != 0) {
                    params.put("files", mGson.toJson(data));
                }
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        HiddenServices.init().saveHiddenInfoAction(params, isEdit);
                    }
                });
            }
        });
    }


    /**
     * 处理所有的网络请求返回
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(final HttpCallbackEvent event) {
        if (EventTag.TAG_HIDDEN_SELF_CHECK_TYPE == event.getTag()) {//隐患自查类型
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    List<SelfCheckType> result = (List<SelfCheckType>) event.getObj();
                    if (result != null && !result.isEmpty()) {
                        aSelfTypeName = new String[result.size()];
                        aSelfTypeId = new int[result.size()];
                        for (int i = 0; i < result.size(); i++) {
                            aSelfTypeName[i] = result.get(i).getTypeName();
                            aSelfTypeId[i] = result.get(i).getTypeId();
                        }
                        showSelfCheckTypeDialog();
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (EventTag.TAG_HIDDEN_LEVEL == event.getTag()) {//隐患级别
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    List<HiddenLevel> result = (List<HiddenLevel>) event.getObj();
                    if (result != null && !result.isEmpty()) {
                        aHiddenLevelName = new String[result.size()];
                        aHiddenLevelId = new int[result.size()];
                        for (int i = 0; i < result.size(); i++) {
                            aHiddenLevelName[i] = result.get(i).getLevelName();
                            aHiddenLevelId[i] = result.get(i).getLevelId();
                        }
                        showHiddenLevelDialog();
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (EventTag.TAG_HIDDEN_TYPE == event.getTag()) {//隐患分类
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    mHiddenTypes = (List<HiddenType>) event.getObj();
                    if (mHiddenTypes != null && !mHiddenTypes.isEmpty()) {
                        showHiddenCategoryPickView(mHiddenTypes);
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (EventTag.TAG_HIDDEN_SAVE_ACTION == event.getTag()) {//隐患保存Action
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.show();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.showDoneTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            HiddenMainRefreshEvent refreshEvent = new HiddenMainRefreshEvent();
                            refreshEvent.setTag(EventTag.TAG_HIDDEN_DANGER_MAIN_REFRESH);
                            AppService.getBus().post(refreshEvent);
                            AppManager.getAppManager().finishActivity(HiddenPropActivity.this);
                        }
                    });
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        } else if (EventTag.TAG_HIDDEN_DANGER_ATTEST_LIST == event.getTag()) {//隐患保存Action
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.show();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.hidden();
                    List<UploadFileItem> items = (List<UploadFileItem>) event.getObj();
                    addUploadFileItems(items);
                } else {
                    mHUDUtil.hidden();
                    //mHUDUtil.showErrorTip(event.getMsg());
                }
            }
        }
    }


    @Override
    public void evidencePlusEventHandler(String tag) {
        if (this.getClass().getName().equals(tag)) {
            showActionSheet(EvidenceActionSheetType.CAMERA_VOICE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHUDUtil.hidden();
        mHiddenDangerInfoDao = null;
        AppService.getDBHelper().getDaoSession().clear();

    }

    private void initEditView(HiddenDangerInfo info) {
        if (null == info) return;
        /**
         * 服务器ID
         */
        iHiddenDangerId = info.getHiddenDangerId();

        /**
         * 自查部位
         */
        editCheckPart.setText(StringUtils.nullValue(info.getCheckPart()));
        /**
         * 自查时间
         */
        metCheckDate.setText(DateUtils.getFormatDateStringWithDateString(StringUtils.nullValue(info.getCheckDate()), DateUtils.dateFormatYMD));
        /**
         * 自查类型
         */
        metCheckType.setText(StringUtils.nullValue(info.getSelfCheckName()));
        metCheckType.setTag((StringUtils.nullValue(info.getSelfCheckId() + "")));
        /**
         * 检查人
         */
        editCheckPerson.setText(StringUtils.nullValue(info.getCheckUserName()));

        /**
         * 隐患分类
         */
        metHiddenCate.setText(StringUtils.nullValue(info.getTypeBigName()) + "-" + StringUtils.nullValue(info.getTypeSmallName()));
        txtTypeId = info.getTypeSmallId() + "";
        /**
         * 隐患级别
         */
        metHiddenLevel.setText(StringUtils.nullValue(info.getLevelName()));
        metHiddenLevel.setTag(StringUtils.nullValue(info.getLevelId() + ""));
        txtLevelId = StringUtils.nullValue(info.getLevelId() + "");
        /**
         * 隐患内容
         */
        editHiddenContent.setText(StringUtils.nullValue(info.getHiddenDangerContent()));

        /**
         * 是否上报
         */
        if ("0".equals(StringUtils.nullValue(info.getStatus() + "", "0"))) {
            chkIsReport.setChecked(false);
        } else {
            chkIsReport.setChecked(true);
        }

        /****************************************************************************************************************
         * 整改部分
         ****************************************************************************************************************/
        /**
         * 整改部门
         */
        editReformDepartment.setText(StringUtils.nullValue(info.getReformDepartment()));

        /**
         * 整改负责人
         */
        editReformPerson.setText(StringUtils.nullValue(info.getReformPerson()));

        /**
         * 整改时间
         */
        metReformFinishDate.setText(DateUtils.getFormatDateStringWithDateString(StringUtils.nullValue(info.getPlanFinishDate()), DateUtils.dateFormatYMD));

        /**
         * 投资金额
         */
        editReformFund.setText(StringUtils.nullValue(info.getReformFund()));

        /**
         * 整改措施
         */
        editReformContent.setText(StringUtils.nullValue(info.getReformContent()));
        /**
         * 是否整改
         */
        chkIsReformFinished.setChecked(info.getIsReformfinished());
    }

    private void initTestValue() {
        if (BuildConfig.DEBUG) {
            editCheckPart.setText("测试检查部位-" + DateUtils.getCurrentDate(DateUtils.dateFormatHMS));
//            metCheckDate.setText(DateUtils.getCurrentDate(DateUtils.dateFormatYMDHMS));
            editCheckPerson.setText("测试检查人");
            editHiddenContent.setText("测试隐患描述内容-" + DateUtils.getCurrentDate(DateUtils.dateFormatYMDHMS));
            editReformDepartment.setText("测试整改部门");
            editReformPerson.setText("测试整改负责人");
            editReformFund.setText("100");
            editReformContent.setText("测试整改措施内容-" + DateUtils.getCurrentDate(DateUtils.dateFormatYMDHMS));
        }
    }

}
